package hexapawn.game;

import java.util.LinkedList;
import java.util.List;

public abstract class Player {
	
	public static final int PLAYER_1 		= 1;
	public static final int PLAYER_2 		= 2;
	public static final int PLAYER_MINIMAX 	= 3;
	public static final int PLAYER_MDP 		= 4;
	
	protected List<Piece> pieces;
	protected Board board;
	protected Cell from;
	protected int type;
	
	public Player(Board _board, int player_type){
		board = _board;
		pieces = new LinkedList<Piece>();
		setupPieces(player_type);
		type = player_type;
	}
	
	/**
	 * Sets all the pieces according to player_type. PLAYER_1's
	 * pieces starts at top while PLAYER_2's starts at bottom.
	 * 
	 * @param player_type - The type of player which may take
	 * 					    the following values:
	 * 
	 * PLAYER_1, PLAYER_2, PLAYER_MINIMAX, PLAYER_MDP
	 */
	private void setupPieces(int player_type){
		int start_row;
		switch(player_type){
			case PLAYER_1:
				start_row = 0;
				break;
			case PLAYER_2:
				start_row = board.getRow()-1;
				break;
			case PLAYER_MINIMAX:
				start_row = board.getRow()-1;
				break;
			case PLAYER_MDP:
				start_row = 0;
				break;
			default:
				start_row = 0;
		}
		for(int i=0; i<board.getColumn(); i++){
			Piece p = new Piece(start_row, i, player_type);
			pieces.add(p);
			board.getCells()[start_row][i].setPiece(p);
		}
		from = null;
	}
	
	public int getType(){
		return type;
	}
	
	public List<Piece> getPieces(){
		return pieces;
	}
	
	/**
	 * Moves a Piece object at from (Cell) location to the to (Cell) location.
	 * The from variable should is set during a call to "getPossibleMoves()".
	 * The to cell variable is setup during the call to "makeMove". The Game
	 * object will make sure the to variable is a valid location to move to.
	 * 
	 * @param to - The Cell object to move to
	 */
	public void makeMove(Cell to){
		if(from == null) return;	//Do nothing if from is not set
		if(to.getColumn() - from.getColumn() == 0){		//Has no target to eat. Move 1 step only.
			to.setPiece(from.getPiece());
			from.removePiece();
		}else{ //Eating the diagonal target
			removeTarget(to);
			to.setPiece(from.getPiece());
			from.removePiece();
		}
	}
	
	/**
	 * Removes the target Cell in the Player's pieces array. This method
	 * is used when a Player "takes" another player's Piece object.
	 * 
	 * @param target
	 */
	protected abstract void removeTarget(Cell target);
	
	public abstract List<Cell> getPossibleMoves(Cell _from);

}
